package ru.ifmo.segal.algorithms;

import ru.ifmo.segal.Algorithm;
import ru.ifmo.segal.InitialConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * @author avhaliullin
 */
public class Explicit implements Algorithm {
    private final boolean backward;

    public Explicit(boolean backward) {
        this.backward = backward;
    }

    private Double safeGet(List<Double> list, int index) {
        return index < 0 || index >= list.size() ? 0 : list.get(index);
    }

    public List<Double> step(InitialConfig config, int stepNum, List<Double> prev) {
        double dx = config.getDistanceSegmentation().getStep();
        double dt = config.getTimeSegmentation().getStep();
        double u = config.getU();
        double kappa = config.getKappa();        //kappa

        List<Double> cur = new ArrayList<Double>();
        for (int dist = 0; dist < config.getDistanceSegmentation().getCount(); dist++) {
            cur.add((u * (backward ? (safeGet(prev, dist - 1) - safeGet(prev, dist)) : (safeGet(prev, dist) - safeGet(prev, dist + 1))) / dx +
                    kappa * (safeGet(prev, dist - 1) - 2 * safeGet(prev, dist) + safeGet(prev, dist + 1)) / Math.pow(dx, 2)) * dt +
                    safeGet(prev, dist));
        }
        return cur;
    }
}
